#include <iostream>
#include <vector>
using namespace std;

class Solution {
public:
    int search(vector<int>& nums, int target) {
        int left = 0;
        int right = nums.size() - 1;
        while (left <= right)
        {
            int mid = (right + left) / 2;
            if (nums[mid] > target)
            {
                right = mid - 1;
            }
            else if (nums[mid] < target)
            {
                left = mid + 1;
            }
            else
            {
                return mid;
            }
        }
        return -1;
    }
};

class Solution {
public:
    vector<int> searchRange(vector<int>& nums, int target) {
        if (nums.size() == 0)
        {
            return { -1,-1 };
        }
        int left = 0;
        int right = nums.size() - 1;
        vector<int> ret;
        int l = -1;
        int r = -1;
        while (left < right)
        {
            int mid = (left + right) / 2;
            if (nums[mid] >= target)
            {
                right = mid;
            }
            else
            {
                left = mid + 1;
            }
        }
        if (nums[left] == target)
        {
            l = left;
        }
        left = 0;
        right = nums.size() - 1;
        while (left < right)
        {
            int mid = (left + right + 1) / 2;
            if (nums[mid] > target)
            {
                right = mid - 1;
            }
            else
            {
                left = mid;
            }
        }
        if (nums[left] == target)
        {
            r = left;
        }
        ret.push_back(l);
        ret.push_back(r);
        return ret;
    }
};

class Solution {
public:
    int searchInsert(vector<int>& nums, int target)
    {
        size_t n = size(nums);
        int left = 0;
        int right = n - 1;
        while (left < right)
        {
            int mid = (left + right) / 2;
            if (nums[mid] >= target)
            {
                right = mid;
            }
            else
            {
                left = mid + 1;
            }
        }
        if (nums[left] < target)
        {
            return left + 1;
        }
        return left;
    }
};

class Solution {
public:
    int mySqrt(int x) {
        long long left = 0;
        int right = x - 1;
        while (left <= right)
        {
            long long mid = (left + right) / 2;
            if (mid * mid > x)
            {
                right = mid - 1;
            }
            else if (mid * mid < x)
            {
                left = mid + 1;
            }
            else
            {
                return mid;
            }
        }
        if (left * left > x)
        {
            return left - 1;
        }
        return left;
    }
};

class Solution {
public:
    int peakIndexInMountainArray(vector<int>& arr) {
        int n = arr.size();
        int left = 0;
        int right = n - 1;
        while (left < right)
        {
            int mid = (left + right) / 2;
            if (arr[mid] < arr[mid + 1])
            {
                left = mid + 1;
            }
            else if (arr[mid] > arr[mid + 1])
            {
                right = mid;
            }
        }
        return left;
    }
};

class Solution {
public:
    int findMin(vector<int>& nums) {
        int left = 0;
        int right = nums.size() - 1;
        int x = nums[right];
        while (left < right)
        {
            int mid = (left + right) / 2;
            if (nums[mid] < nums[mid + 1] && nums[mid] < x)
            {
                right = mid;
            }
            else
            {
                left = mid + 1;
            }
        }
        return nums[left];
    }
};

class Solution {
public:
    int takeAttendance(vector<int>& records) {
        int left = 0;
        int right = records.size() - 1;
        while (left < right)
        {
            int mid = (left + right) / 2;
            if (records[mid] == mid)
            {
                left = mid + 1;
            }
            else
            {
                right = mid;
            }
        }
        return left == records[left] ? left + 1 : left;
    }
};